package com.hwua.view;

import com.hwua.entity.Course;
import com.hwua.entity.Teacher;
import com.hwua.service.CourseService;
import com.hwua.service.TeacherService;
import com.hwua.service.impl.CourseServiceImpl;
import com.hwua.service.impl.TeacherServiceImpl;
import com.hwua.tqm.FatherTable;

import java.util.List;

/**
 * @author CQY
 */
public class CourseView extends FatherTable {

    /**
     * 设置表格的列名
     * @return
     */
    @Override
    public String[] columnNamesInit() {
        String[] s={"课程编号","课程名","教师编号"};
        return s;
    }

    /**
     * 数据初始化
     * @return
     */
    @Override
    public Object[][] tableDataInit() {
        CourseService service=new CourseServiceImpl();
        List<Course> list=service.findAll();
        int row=list.size();
        Object[][] date=new Object[row][3];
        for (int i = 0; i <date.length ; i++) {
           Course course=list.get(i);
          date[i][0]= course.getCourseId();
          date[i][1]=course.getCourseName();
          date[i][2]=course.getTeaId();

        }
        return date;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteMoreAction(Object[] ids) {
        CourseService service=new CourseServiceImpl();
        for (int i = 0; i <ids.length ; i++) {
            Course course=new Course();
            service.delete((String)ids[i]);
        }


    }

    @Override
    public void addSaveAction(Object[] obj) {
        CourseService service=new CourseServiceImpl();
        String id=(String) obj[0];
        String name=(String)obj[1];
        String teaid=(String)obj[2];
        Course course=new Course(id,name,teaid);
        int row=service.save(course);
    }

    /**
     * 修改信息
     * @param obj
     */
    @Override
    public void updateSaveAction(Object[] obj) {

        CourseService service=new CourseServiceImpl();
        String id=(String) obj[0];
        String name=(String)obj[1];
        String teaid=(String)obj[2];
        Course course=new Course(id,name,teaid);
        int row=service.update(course);
    }

    @Override
    public Object[][] searchAction(String keyword) {
        CourseService service=new CourseServiceImpl();
        List<Course> list=service.findSearch(keyword);
        int row=list.size();
        Object[][] date=new Object[row][3];
        for (int i = 0; i <date.length ; i++) {
            Course course=list.get(i);
            date[i][0]= course.getCourseId();
            date[i][1]=course.getCourseName();
            date[i][2]=course.getTeaId();

        }
        return date;
    }

    @Override
    public void deleteAction(Object id) {

        CourseService service=new CourseServiceImpl();
        service.delete((String)id);
    }
}
